package com.example.sxt_practise.thread;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 集合类不安全
 * @author gaofeng
 * @date 2022年06月04日 10:36
 */
public class ListTest {
    public static void main(String[] args) {
        /**
         * 并发,ArrayList不安全,
         * 1. List<String>   names = Collections.synchronizedList(names) ;
         * 2. List<String> names = new CopyOnWriteArrayList<>() ;
         * 3. List<String> names = new Vector<>() ;
         */
        List<String> names = new CopyOnWriteArrayList<>() ;
        names = Collections.synchronizedList(names) ;
        for (int i = 0; i < 10; i++) {
            List<String> finalNames = names;
            new Thread(() -> {
                finalNames.add(UUID.randomUUID().toString()) ;
                System.out.println(finalNames);
            }).start(); ;
        }
    }
}
